package com.idanchuang.component.core.json;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.io.IOException;
import java.util.List;

/**
 *
 * Jackson序列化工具类
 *
 * 自定义配置方式: Jackson.getObjectMapper().configure(..).configure(..);
 *
 * @author yjy
 * Created at 2022/4/20 1:47 下午
 */
public class Jackson {

    private static final ObjectMapper MAPPER;

    static {
        MAPPER = new ObjectMapper().findAndRegisterModules();
        // 当出现 Java 类中未知的属性时不报错，而是忽略此 JSON 字段
        MAPPER.configure(SerializationFeature.FAIL_ON_UNWRAPPED_TYPE_IDENTIFIERS, false);
    }

    /**
     * 将对象序列化为json字符串
     * @param value 对象
     * @return ..
     */
    public static String toJsonString(Object value) {
        try {
            if (value == null) {
                return null;
            }
            return MAPPER.writeValueAsString(value);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 将json字符串反序列化为对象
     * @param jsonString 字符串
     * @param tClass 指定对象类型
     * @param <T> ..
     * @return 对象
     */
    public static <T> T parseObject(String jsonString, Class<T> tClass) {
        try {
            if (jsonString == null) {
                return null;
            }
            return MAPPER.readValue(jsonString, tClass);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 将json字符串反序列化为对象，用于带泛型的对象
     * @param jsonString 字符串
     * @param tTypeReference 指定对象类型
     * @param <T> ..
     * @return 对象
     */
    public static <T> T parseObject(String jsonString, TypeReference<T> tTypeReference) {
        try {
            if (jsonString == null) {
                return null;
            }
            return MAPPER.readValue(jsonString, tTypeReference);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * String列表
     */
    private static class StringListTypeReference extends TypeReference<List<String>> {}

    /**
     * 将json字符串反序列化为对象，用于带泛型的对象
     * @param jsonString 字符串
     * @return 字符串列表
     */
    public static List<String> parseListString(String jsonString) {
        try {
            if (jsonString == null) {
                return null;
            }
            return MAPPER.readValue(jsonString, new StringListTypeReference());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * @return 基础ObjectMapper
     */
    public static ObjectMapper getObjectMapper() {
        return MAPPER;
    }


//    private static class MyListTypeReference extends TypeReference<List<EnvConfigBean>> {}
//
//    public static void main(String[] args) {
//        EnvConfigBean dto = new EnvConfigBean();
//        // 序列化
//        String jsonString = Jackson.toJsonString(dto);
//        // 反序列化为普通对象
//        EnvConfigBean res = Jackson.parseObject(jsonString, EnvConfigBean.class);
//        System.out.println(res);
//
//        // 反序列化为嵌套泛型的复杂对象
//        String jsonString1 = "[]";
//        List<EnvConfigBean> list = Jackson.parseObject(jsonString1, new MyListTypeReference());
//        System.out.println(list);
//
//    }

}
